using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO;
using Speed;

namespace SpeedSearch
{

    class Program : Speed.Windows.ProgramBase
    {

        /// <summary>
        /// Diretório AppData/SpeedSearch
        /// </summary>
        public static string DirRoot;
        public static string DirData;
        //public static string DirIndexes;
        public static string DataFile;
        public static Database Db;
        /// <summary>
        /// Versão do índice Lucene
        /// </summary>
        public static Lucene.Net.Util.Version LVersion = Lucene.Net.Util.Version.LUCENE_30;
        public static string DbVersion = "1.1";

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            //GenetateTestes();

            Log.Clear();

#if DEBUG
            Program.IsDebug = true;
            Log.IsInfoEnabled = true;
#else
            Log.IsInfoEnabled = false;
            Program.IsDebug = false;
#endif
            Title = "SpeedSearch";

            DirRoot = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "SpeedSearch");
            DirData = Path.Combine(DirRoot, "Data");
            DataFile = GetDataFile("Config.dat");

            Db = new Database();

            bool test = false;
#if !DEBUG
            test = false;
#endif
            if (test)
            {
                //IndexHelper.CreateIndexDev("C:\\Dev");
                //IndexHelper.ReindexAll();
                //return;

                //var row18 = Db.Indexes.FirstOrDefault(p => p.IndexName.Contains("v.2.01.018"));
                //IndexHelper.GenerateIndex(row18);

                // Application.Run(new Forms.FormSearch());
                Application.Run(new Forms.FormIndexes());
                // Application.Run(new Forms.FormReindex());
            }
            else
            {
                Application.Run(new FormMain());
            }
        }

        public static string GetDataFile(string name)
        {
            return Path.Combine(DirData, name);
        }

        public static string GetIndexFile(string name)
        {
            return Path.Combine(Db.Parameters.IndexDirectory, name);
        }

        static void GenetateTestes()
        {
            int fileCount = 100;
            int size = 80 * 1024 * 1000;
            string dir = @"H:\Temp\Indexes";
            string buffer = new string('a', size);
            for (int i = 1; i <= fileCount; i++)
            {
                string fileName = Path.Combine(dir, "File" + i + ".txt");
                if (File.Exists(fileName))
                    File.Delete(fileName);
                File.WriteAllText(fileName, buffer);
            }
        }

    }

}
